# Copyright (c) 2024, Vnimy and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document
from frappe.custom.doctype.custom_field.custom_field import create_custom_field
from frappe.custom.doctype.property_setter.property_setter import (
	make_property_setter, delete_property_setter)

custom_fields = {
	'enable_tracking_number': [
		{
			"doctype": "Custom Field",
			"dt": "Job Card",
			"fieldname": "tracking_section",
			"fieldtype": "Section Break",
			"insert_after": "process_loss_qty",
			"label": "",
			"modified": "2025-03-20 22:12:02.049024",
			"name": "Job Card-tracking_section"
		},		
		{
			"doctype": "Custom Field",
			"dt": "Job Card",
			"fieldname": "tracking_number",
			"fieldtype": "Data",
			"options": "Barcode",
			"insert_after": "tracking_section",
			"label": "Scan Tracking Number",
			"modified": "2025-03-20 22:12:02.049024",
			"name": "Job Card-tracking_number"
		},
		{
			"doctype": "Custom Field",
			"dt": "Job Card",
			"fieldname": "tracking_details",
			"fieldtype": "Table",
			"options": "Tracking Detail",
			"depends_on": "eval:doc.tracking_details && doc.tracking_details.length",
			"insert_after": "tracking_number",
			"label": "Tracking Details",
			"modified": "2025-03-20 22:12:02.049024",
			"name": "Job Card-tracking_details"
		}
	],
	'enable_meter_per_roll': [
		{
			"doctype": "Custom Field",
			"dt": "Work Order",
			"fieldname": "meter_per_roll",
			"fieldtype": "Int",
			"insert_after": "produced_qty",
			"depends_on": "eval:doc.stock_uom=='米'",
			"read_only": 1,
			"label": "Meter Per Roll",
			"modified": "2025-03-15 22:12:02.049024",
			"name": "Work Order-meter_per_roll"
		}
	],	
	"enable_workstation_employee_group_link":[
		{
			"doctype": "Custom Field",
			"dt": "Employee Group",
			"fieldname": "workstation",
			"fieldtype": "Link",
			"options": "Workstation",
			"insert_after": "employee_group_name",
			"label": "Workstation",
			"modified": "2025-03-11 22:15:02.049023",
			"name": "Employee Group-workstation"
		}
	],
	"enable_item_attribute_value_field": [
		{
			"doctype": "Custom Field",
			"dt": "Item Attribute",
			"fieldname": "item_custom_field",
			"fieldtype": "Link",
			"options": "Custom Field",
			"insert_after": "numeric_values",
			"label": "Item Custom Field",
			"modified": "2025-03-19 22:15:02.049023",
			"name": "Item Attribute-item_custom_field"
		}
	],
	"enable_auto_consumption": [],
	"enable_spec_product_type": [
		{
			"doctype": "Custom Field",
			"dt": "Item",
			"fieldname": "spec",
			"fieldtype": "Data",			
			"insert_after": "item_name",
			"read_only": 1,
			"label": "Spec",
			"modified": "2025-03-26 22:15:02.049023",
			"name": "Item-spec"
		},
		{
			"doctype": "Custom Field",
			"dt": "Item",
			"fieldname": "product_type",
			"fieldtype": "Data",
			"read_only": 1,			
			"insert_after": "spec",
			"label": "Product Type",
			"modified": "2025-03-26 22:15:02.049023",
			"name": "Item-product_type"
		},
		{
			"doctype": "Custom Field",
			"dt": "BOM",
			"fieldname": "spec",
			"fieldtype": "Data",			
			"insert_after": "set_rate_of_sub_assembly_item_based_on_bom",
			"read_only": 1,
			"label": "Spec",
			"fetch_from": "item.spec",
			"modified": "2025-03-26 22:15:02.049023",
			"name": "BOM-spec"
		},
		{
			"doctype": "Custom Field",
			"dt": "BOM",
			"fieldname": "product_type",
			"fieldtype": "Data",
			"read_only": 1,			
			"insert_after": "spec",
			"label": "Product Type",
			"fetch_from": "item.product_type",
			"modified": "2025-03-26 22:15:02.049023",
			"name": "BOM-product_type"
		},
		{
			"doctype": "Custom Field",
			"dt": "Work Order",
			"fieldname": "spec",
			"fieldtype": "Data",			
			"insert_after": "item_name",
			"read_only": 1,
			"label": "Spec",
			"fetch_from": "production_item.spec",
			"modified": "2025-03-26 22:15:02.049023",
			"name": "Work Order-spec"
		},
		{
			"doctype": "Custom Field",
			"dt": "Work Order",
			"fieldname": "product_type",
			"fieldtype": "Data",
			"read_only": 1,			
			"insert_after": "spec",
			"label": "Product Type",
			"fetch_from": "production_item.product_type",
			"modified": "2025-03-26 22:15:02.049023",
			"name": "Work Order-product_type"
		},
		{
			"doctype": "Custom Field",
			"dt": "Job Card",
			"fieldname": "spec",
			"fieldtype": "Data",			
			"insert_after": "item_name",
			"read_only": 1,
			"label": "Spec",
			"fetch_from": "production_item.spec",
			"modified": "2025-03-26 22:15:02.049023",
			"name": "Job Card-spec"
		},
		{
			"doctype": "Custom Field",
			"dt": "Job Card",
			"fieldname": "product_type",
			"fieldtype": "Data",
			"read_only": 1,			
			"insert_after": "spec",
			"label": "Product Type",
			"fetch_from": "production_item.product_type",
			"modified": "2025-03-26 22:15:02.049023",
			"name": "Job Card-product_type"
		},							
	],
	"enable_time_in_mins":[]
}


class LightMesSettings(Document):
	def on_update(self):
		before_save = self.get_doc_before_save()
		for key in custom_fields.keys():
			if (not before_save or before_save.get(key) != self.get(key)):
				frappe.cache().delete_value(key)
				if self.get(key):
					self.create_custom_fields(key)
				else:
					self.remove_custom_fields(key)
		if (not before_save or before_save.enable_time_in_mins != self.enable_time_in_mins):
			if self.enable_time_in_mins:
				make_property_setter("Job Card Time Log", "time_in_mins", "read_only", 0, "Check")
			else:
				delete_property_setter("Job Card Time Log", "read_only", "time_in_mins")
					

	def create_custom_fields(self, key):
		fields = custom_fields.get(key) or []
		for df in fields:
			if not frappe.db.exists('Custom Field', df.get('name')):
				create_custom_field(df.get('dt'), df, ignore_validate=True)

	def remove_custom_fields(self, key):
		fields = custom_fields.get(key) or []
		for df in fields:
			if frappe.db.exists('Custom Field', df.get('name')):
				frappe.delete_doc("Custom Field", df.get('name'))